package com.dabai.videobrowser.other;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.dabai.dbutils.utils.DabaiUtils;
import com.dabai.uitools.utils.ViewUtils;
import com.dabai.videobrowser.R;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.textfield.TextInputLayout;

import java.util.ArrayList;
import java.util.Collections;

/**
 * Description : Other
 *
 * @author BAI
 */
public class MainUtils {


    @SuppressLint("Range")
    public static void showGenerateHistory(Context context, TextInputLayout textInputLayout) {

        //数据库创建与初始化
        SQLiteDatabase db = context.openOrCreateDatabase("HistoricalRecord", Context.MODE_PRIVATE, null);
        String create = "create table if not exists GenerateHistoricalRecord(id integer primary key autoincrement,content text not null unique);";
        db.execSQL(create);


        ArrayList<String> arrayList = new ArrayList<>();

        String search = "select * from GenerateHistoricalRecord";
        Cursor cursor = db.rawQuery(search, null);

        while (cursor.moveToNext()) {
            String text = cursor.getString(cursor.getColumnIndex("content"));
            arrayList.add(text);

        }
        cursor.close();

        if (arrayList.isEmpty()) {
            return;
        }


        Collections.reverse(arrayList);

        String[] items = arrayList.toArray(new String[0]);

        new MaterialAlertDialogBuilder(context)
                .setTitle("扫描过的目录")
                .setItems(items, (dialog, which) -> {

                    textInputLayout.getEditText().setText(items[which]);

                })
                .setPositiveButton("确认", null)
                .setNeutralButton("清空", (dialog, which) -> {
                    delTable(db, "GenerateHistoricalRecord");
                    DabaiUtils.showToast(context, "已清空");
                })
                .show().getWindow().setWindowAnimations(R.style.DBAnim_Transitions_Via);

    }



    public static void delTable(SQLiteDatabase db, String tableName) {
        String sql = "delete from " + tableName;
        String sql2 = "update sqlite_sequence SET seq =0 where name ='" + tableName + "'";//自增长ID为0

        db.execSQL(sql2);
        db.execSQL(sql);
    }

}
